package com.ophiux.system.config;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @desc: sentinel规则配置，两种方式：①通过服务注入 ②通过配置文件加载
 * @author: hhl
 * @date：2023/5/23 10:19
 */
//@Component
public class SentinelRuleConfig {

    @Bean
    public void init(){
        List<DegradeRule> rules = new ArrayList<>();
        DegradeRule rule = new DegradeRule("testSentinelSlowRequest")
                // 熔断策略，支持慢调用比例/异常比例/异常数策略
                .setGrade(RuleConstant.DEGRADE_GRADE_RT)
                // Max allowed response time    慢调用比例模式下为慢调用临界 RT（超出该值计为慢调用）；异常比例/异常数模式下为对应的阈值
                .setCount(10)
                // Retry timeout (in second) 熔断时长，单位为 s
                .setTimeWindow(1)
                // 熔断触发的最小请求数，请求数小于该值时即使异常比率超出阈值也不会熔断
                .setMinRequestAmount(1)
                // 统计时长（单位为 ms），如 60*1000 代表分钟级
                .setStatIntervalMs(1000)
                // Circuit breaker opens when slow request ratio > 60%  慢调用比例阈值，仅慢调用比例模式有效
                .setSlowRatioThreshold(0.1);

        DegradeRule rule2 = new DegradeRule("testSentinelExceptionRatio")
                // 熔断策略，支持慢调用比例/异常比例/异常数策略
                .setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO)
                // Max allowed response time    慢调用比例模式下为慢调用临界 RT（超出该值计为慢调用）；异常比例/异常数模式下为对应的阈值
                .setCount(0.1)
                // Retry timeout (in second) 熔断时长，单位为 s
                .setTimeWindow(1)
                // 熔断触发的最小请求数，请求数小于该值时即使异常比率超出阈值也不会熔断
                .setMinRequestAmount(1)
                // 统计时长（单位为 ms），如 60*1000 代表分钟级
                .setStatIntervalMs(1000)
                // Circuit breaker opens when slow request ratio > 60%  慢调用比例阈值，仅慢调用比例模式有效
                .setSlowRatioThreshold(0.1);

        DegradeRule rule3 = new DegradeRule("testSentinelExceptionCount")
                // 熔断策略，支持慢调用比例/异常比例/异常数策略
                .setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT)
                // Max allowed response time    慢调用比例模式下为慢调用临界 RT（超出该值计为慢调用）；异常比例/异常数模式下为对应的阈值
                .setCount(0.4)
                // Retry timeout (in second) 熔断时长，单位为 s
                .setTimeWindow(1)
                // 熔断触发的最小请求数，请求数小于该值时即使异常比率超出阈值也不会熔断
                .setMinRequestAmount(3)
                // 统计时长（单位为 ms），如 60*1000 代表分钟级
                .setStatIntervalMs(1000)
                // Circuit breaker opens when slow request ratio > 60%  慢调用比例阈值，仅慢调用比例模式有效
                .setSlowRatioThreshold(0.1);

        rules.add(rule);
        rules.add(rule2);
        rules.add(rule3);
        DegradeRuleManager.loadRules(rules);
        System.out.println("Degrade rule loaded: " + rules);


        //限流规则
        List<FlowRule> flowRuleList = new ArrayList<>();
        FlowRule flowRule = new FlowRule("testSentinelFlow").setGrade(RuleConstant.FLOW_GRADE_QPS).setCount(1).setStrategy(RuleConstant.STRATEGY_DIRECT);
        flowRuleList.add(flowRule);
        FlowRuleManager.loadRules(flowRuleList);
        System.out.println("Flow rule loaded: " + flowRuleList);

    }
}
